/* 
 * File:   Console.cpp
 * Author: ja1e09
 * 
 * Created on July 28, 2011, 6:30 PM
 */

#include "Console.h"

Console* Console::pInstance = 0;

Console::Console(void)
{
}

Console*
Console::GetInstance(void)
{
    if (pInstance == 0)
        pInstance = new Console;

    return pInstance;
}

void Console::Destroy(void)
{
    if (pInstance != 0)
    {
        // Empty the console
        Clear();

        SAFE_DELETE(pInstance);
    }
}

void Console::Clear()
{
    if (vConsole.size() == 0)
        return;

    std::vector<CConsoleItem*>::iterator ConsoleIter;
    for (ConsoleIter = vConsole.begin(); ConsoleIter != vConsole.end(); ++ConsoleIter)
        SAFE_DELETE(*ConsoleIter);

    vConsole.clear();
}

void Console::PrintItem(CConsoleItem* pConsoleItem)
{
    struct tm* TStamp = pConsoleItem->GetTS();
    std::cout << TStamp->tm_hour << ":" << TStamp->tm_min << ":" << TStamp->tm_sec << " " << pConsoleItem->GetRank() << " " << pConsoleItem->GetMsg() << std::endl;
}

void Console::Add(std::string sMsg)
{
    // Create a new Console Item
    CConsoleItem* pConsoleItem = new CConsoleItem(iRank,sMsg);

    // Add it to the vector
    vConsole.push_back(pConsoleItem);

    // Print
    PrintItem(pConsoleItem);
}
